<template>
  <div>
    <div class="login">
        <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="登录账号" prop="userName">
            <el-input v-model.trim="loginForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="passWord">
            <el-input type="password" v-model.trim="loginForm.passWord" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleLogin('loginForm')">提交</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    var validateName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入登录账号'));
        }
        else {
            callback();
        }
    }
    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入登录密码'));
        }
        else {
            callback();
        }
    }

    return {
        loginForm: {
            userName: '',
            passWord: ''
        },
        rules: {
            userName: [
                { validator: validateName, trigger: 'blur' }
            ],
            passWord: [
                { validator: validatePass, trigger: 'blur' }
            ]
        }
    }
  },
  methods: {
      handleLogin(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
    width: 400px;
    margin: auto;
    border: #FF0000 solid 1px;
}
</style>
